<template>
    <div class="container">
        <h3>
            欢迎来到 IP-任你流
            <h6>后台管理</h6>
        </h3>
        <div class="tips">
            Tips:
            <p>
                还在担心不知道对象在哪鬼混吗?
                使用用站即可获取对象IP,查询到你的对象在哪玩!
            </p>
            <p>1.先获取标识,生成网址,再发送给目标对象,获取其ip信息</p>
            <p>2.生成网址后可自己打开预览</p>
            <p>3.查询到ip,再通过ip查询目标地理位置</p>
        </div>
        <form class="form">
            <div class="form_input">
                <label>获取标识</label>
                <input
                    v-model="digit"
                    type="text"
                    placeholder="请输入查询标识位数,默认32位, 建议保存后续查询结果使用"
                />
                <button type="button" class="btn" @click="clickHandle('4')">
                    获取
                </button>
            </div>
            <div class="form_input">
                <label>获取目标信息网址</label>
                <input
                    id="codeBox"
                    v-model="url"
                    type="text"
                    placeholder="将网址发送给目标,诱导其打开即可"
                />
                <button type="button" class="btn" @click="clickHandle('5')">
                    复制
                </button>
            </div>
            <div class="form_input">
                <label>查询ip信息</label>
                <input v-model="kw" type="text" placeholder="请输入查询标识" />
                <button type="button" class="btn" @click="clickHandle('1')">
                    提交
                </button>
            </div>
            <div class="form_input">
                <label>查询地理位置(精确到街道级)</label>
                <!-- <input
            v-model="ipInfo"
            type="text"
            placeholder="(暂不提供服务,接口太贵, https://www.ipdatacloud.com/)复制上面查询结果的ip,查询目标地址"
          /> -->
                <div>
                    <a
                        class="ipysj"
                        href="https://www.ipdatacloud.com/"
                        target="_blank"
                        >IP数据云</a
                    >
                    <br />
                    <a
                        class="ipysj"
                        href="https://www.chaipip.com/"
                        target="_blank"
                        >chaipip(基于埃文科技)</a
                    >
                </div>
                <!-- <button class="btn" @click="clickHandle('2')">提交</button> -->
            </div>
            <div class="form_input">
                <label>删除数据</label>
                <input
                    v-model="p"
                    class="ipt"
                    type="password"
                    placeholder="请输入密码, 作者专属"
                />
                <button type="button" class="btn" @click="clickHandle('3')">
                    提交
                </button>
            </div>
            <div class="form_input">
                <label>查询所有数据</label>
                <input
                    v-model="p2"
                    type="password"
                    placeholder="请输入密码, 作者专属"
                />
                <button type="button" class="btn" @click="clickHandle('0')">
                    提交
                </button>
            </div>
        </form>
        <!-- 渲染ip信息列表 -->
        <ul class="xr" v-if="!ipInfoList || ipInfoList.length != 0">
            <li>IP_信息_查询标识_获取时间</li>
            <li v-for="item in ipInfoList" :key="item.t">
                {{
                    "😀" +
                    item.ip +
                    "_" +
                    item.info +
                    "_" +
                    item.kw +
                    "_" +
                    formDateAt(item.t)
                }}
            </li>
        </ul>
        <div class="xr" v-else>暂无数据,请点击查询ip信息</div>
        <!-- 渲染ip信息列表 -->
        <div v-if="!isHomwPwd" ref="container_pwd" class="container_pwd">
            <div>天王盖地虎</div>
            <input
                v-model="homePwd"
                focus
                ref="pwd_ipt"
                class="pwd_ipt"
                type="text"
                placeholder="请输入密码,你懂任你流的!"
                @keydown.enter="pwdClickHandle"
            />
            <button class="pwd_btn" @click="pwdClickHandle">确定</button>
        </div>
        <div v-if="!isHomwPwd" ref="pwd_zz" class="pwd_zz"></div>
    </div>
</template>

<script setup lang="ts">
import dayjs from "dayjs";
import {randomString} from '@/utils/index';

// import { getIpInfo, deleteIpInfo, getPwd } from '@/api/ip'
const ipInfoList = reactive([])
const digit = ref(); // 标识位数
let kw = ""; // 查询标识
let url = ""; // 网址
let ipInfo = ""; // ip信息
let p = ""; // 密码
let p2 = ""; // 密码
const homePwd = ref<string>() // 首页密码
const isHomwPwd = ref<string>()
onMounted(() => {
  isHomwPwd.value = localStorage.getItem("pwdpwd") || "";
});

// 比对密码
const pwdClickHandle = async () => {
    if (homePwd.value !== '123456') return
    isHomwPwd.value = '123456'
};

// 处理提交数据
const clickHandle = async (e: string) => {
    if (e === "1") {
        // // 查询ip信息
        // const d = { kw: kw };
        // const res = await getIpInfo(d);
        // kw = "";
        // // console.log(res, 'res')
        // ipInfoList = res;
    } else if (e === "2") {
        // 查询地理位置
    } else if (e === "3") {
        // if (!p) return;
        // // 删除数据
        // const d3 = { p: p };
        // const result = await deleteIpInfo(d3);
        // if (result) {
        //     // console.log('删除成功')
        // }
        // p = "";
    } else if (e === "4") {
        // 获取标识
        const result = randomString(digit.value);
        // console.log(result, '标识')
        digit.value = result;
        // 生成网址
        if (window.location.href.indexOf("art.leishennb.icu") === -1) {
            url = `http://localhost?kw=${result}`;
        } else {
            url = `http://art.leishennb.icu?kw=${result}`; // 生产环境
        }
    } else if (e === "5") {
        // 复制网址
        // navigator.clipboard属性返回 Clipboard 对象，所有操作都通过这个对象进行。
        // 如果navigator.clipboard属性返回undefined，就说明当前浏览器不支持这个 API。
        const clipboardObj = navigator.clipboard;
        if (clipboardObj) {
            await navigator.clipboard.writeText(url);
            console.log("复制成功");
        }
    } else if (e === "0") {
        // if (!p2) return;
        // // 查询所有ip信息
        // const d = { p: p2 };
        // const res = await getIpInfo(d);
        // p2 = "";
        // // console.log(res, 'res')
        // ipInfoList = res.data.ipList;
    }
};
// 格式化时间
const formDateAt = (d: string) => {
    return dayjs(d).format("YYYY-MM-DD HH:mm:ss");
};
</script>

<style lang="scss" scoped>
@media (max-width: 768px) {
    .container {
        width: 90% !important;
    }

    .form_input {
        flex-direction: column;
    }

    label,
    .btn {
        width: auto !important;
    }

    .ipt {
        width: auto !important;
        margin-left: unset !important;
    }

    input {
        margin-left: unset !important;
    }

    label,
    .btn,
    .ipt {
        margin: 4px 0 !important;
    }
}

.container {
    width: 1200px;
    padding: 18px;
    margin: 0 auto;

    h3 {
        text-align: center;
    }
}

.tips {
    margin: 38px 0;
}

.form,
.tips,
.xr {
    padding: 8px;
    border-radius: 8px;
    background: linear-gradient(60deg, #67bed4, rgb(210, 154, 171));
}

.form_input {
    display: flex;
    align-items: center;
    margin: 8px 0;
    padding: 8px;

    label {
        width: 8%;
        padding-right: 18px;
    }

    input {
        display: block;
        width: 88%;
        padding: 8px;
        margin-left: auto;
        border-radius: 8px;
    }

    .btn {
        padding: 8px;
        margin-left: 8px;
        width: 4%;
        border-radius: 8px;
        border: none;
        cursor: pointer;
    }
}

.xr {
    text-align: center;
    margin-top: 18px;
}

.ipysj {
    text-decoration: underline;
    color: #aa3f68;
}

.container_pwd,
.pwd_zz {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.container_pwd {
    padding: 18px;
    background: rgba(255, 255, 255, 0.5);
    z-index: 99;
    text-align: center;
}

.pwd_zz {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
}

.pwd_ipt,
.container_pwd,
.pwd_btn {
    border-radius: 8px;
}

.pwd_ipt {
    display: block;
    padding: 3px;
    margin: 8px 0;
    background: rgb(149, 191, 195);
    opacity: 0.9;
}

.pwd_btn {
    padding: 3px;
    border: 0px solid transparent;
}
</style>
